﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CXJ.Model
{
    public class BaseEntity : Interface.IBaseEntity
    {
        protected string _ConditionString;
        protected object _PK_VALUE;

        private string _SelectSQL = string.Empty;
        private string _InsertSQL = string.Empty;
        private string _DeleteSQL = string.Empty;
        private string _UpdateSQL = string.Empty;

        public BaseEntity() { }

        #region IBaseEntity 成员

        [CXJ.Libiary.DBColumnAttribue(false)]
        public virtual string SelectStatement
        {
            get
            {
                if (string.IsNullOrEmpty(_SelectSQL))
                {
                    string strSQL = "SELECT ";

                    foreach (System.Reflection.PropertyInfo item in this.GetType().GetProperties())
                    {
                        object[] objs = item.GetCustomAttributes(typeof(CXJ.Libiary.DBColumnAttribue), true);
                        if (objs == null || objs.Length == 0) { continue; }
                        CXJ.Libiary.DBColumnAttribue pAttribute = objs[0] as CXJ.Libiary.DBColumnAttribue;
                        if (pAttribute != null && pAttribute.IsDBColumn == true)
                        {
                            if (pAttribute.IsSelect)
                            {
                                strSQL += " [" + item.Name.ToUpper() + "],";
                            }
                        }
                    }
                    strSQL = strSQL.Remove(strSQL.Length - 1, 1);
                    strSQL += " FROM [" + this.TABLE_NAME + "] ";
                    _SelectSQL = strSQL;
                }
                return _SelectSQL;
            }
        }

        [CXJ.Libiary.DBColumnAttribue(false)]
        public virtual string InsertStatement
        {
            get
            {
                if (string.IsNullOrEmpty(_InsertSQL))
                {
                    string strSQL = "INSERT INTO [" + this.TABLE_NAME + "] (";
                    string strInsertHead = " ";
                    string strInsertParm = " ";
                    foreach (System.Reflection.PropertyInfo item in this.GetType().GetProperties())
                    {
                        object[] objs = item.GetCustomAttributes(typeof(CXJ.Libiary.DBColumnAttribue), true);
                        if (objs.Length == 0) { continue; }
                        CXJ.Libiary.DBColumnAttribue pAttribute = objs[0] as CXJ.Libiary.DBColumnAttribue;
                        if (pAttribute != null && pAttribute.IsDBColumn == true)
                        {
                            if (pAttribute.IsInsert)
                            {
                                strInsertHead += " [" + item.Name.ToUpper() + "],";
                                strInsertParm += " @" + item.Name.ToUpper() + ",";
                            }
                        }
                    }
                    strInsertHead = strInsertHead.Remove(strInsertHead.Length - 1, 1);
                    strInsertParm = strInsertParm.Remove(strInsertParm.Length - 1, 1);
                    strSQL += strInsertHead + ") VALUES (" + strInsertParm + ")";
                    _InsertSQL = strSQL;
                }
                return _InsertSQL;
            }
        }

        [CXJ.Libiary.DBColumnAttribue(false)]
        public virtual string UpdateStatement
        {
            get
            {
                if (string.IsNullOrEmpty(_UpdateSQL))
                {
                    string strSQL = "UPDATE  [" + this.TABLE_NAME + "] SET ";

                    foreach (System.Reflection.PropertyInfo item in this.GetType().GetProperties())
                    {
                        object[] objs = item.GetCustomAttributes(typeof(CXJ.Libiary.DBColumnAttribue), true);
                        if (objs.Length == 0) { continue; }
                        CXJ.Libiary.DBColumnAttribue pAttribute = objs[0] as CXJ.Libiary.DBColumnAttribue;
                        if (pAttribute != null && pAttribute.IsDBColumn == true)
                        {
                            if (pAttribute.IsUpdate)
                            {
                                strSQL += " [" + item.Name.ToUpper() + "] =@" + item.Name + ",";
                            }
                        }
                    }
                    strSQL = strSQL.Remove(strSQL.Length - 1, 1);
                    strSQL += " WHERE [" + this.PK + "]=@" + this.PK + " ";
                    _UpdateSQL = strSQL;
                }
                return _UpdateSQL;
            }
        }

        [CXJ.Libiary.DBColumnAttribue(false)]
        public virtual string DeleteStatement
        {
            get
            {
                if (string.IsNullOrEmpty(_DeleteSQL))
                {
                    string strSQL = "DELETE  FROM [" + this.TABLE_NAME + "] ";
                    string strCondition = "";

                    foreach (System.Reflection.PropertyInfo item in this.GetType().GetProperties())
                    {
                        object[] objs = item.GetCustomAttributes(typeof(CXJ.Libiary.DBColumnAttribue), true);
                        if (objs.Length == 0) { continue; }
                        CXJ.Libiary.DBColumnAttribue pAttribute = objs[0] as CXJ.Libiary.DBColumnAttribue;
                        if (pAttribute != null && pAttribute.IsDBColumn == true)
                        {
                            if (pAttribute.IsDelete)
                            {
                                strCondition += " [" + item.Name.ToUpper() + "]=@" + item.Name + ",";
                            }
                        }
                    }
                    if (string.IsNullOrEmpty(strCondition))
                    {//没有设置删除列 则默认为主键
                        strCondition = " [" + this.PK.ToUpper() + "]=@" + this.PK + "";
                    }
                    else
                    {
                        strCondition = strCondition.Remove(strCondition.Length - 1, 1);
                    }
                    strSQL += " WHERE " + strCondition;
                    _DeleteSQL = strSQL;
                }
                return _DeleteSQL;
            }
        }

        [CXJ.Libiary.DBColumnAttribue(false)]
        public virtual string ConditionStatement
        {
            get { return _ConditionString; }
            set { _ConditionString = value; }
        }

        [CXJ.Libiary.DBColumnAttribue(false)]
        public virtual string PK
        {
            get { return ""; }
        }

        [CXJ.Libiary.DBColumnAttribue(false)]
        public virtual object PK_VALUE
        {
            get { return null; }
            set { _PK_VALUE = value; }
        }

        [CXJ.Libiary.DBColumnAttribue(false)]
        public virtual string TABLE_NAME
        {
            get;
            set;
        }

        [CXJ.Libiary.DBColumnAttribue(false)]
        public virtual IList<CXJ.Interface.IBaseEntity> DeleteChildItem
        {
            get { return null; }
        }

        public virtual CXJ.Interface.IBaseEntity[] ChildItem
        {
            get;
            set;
        }

        #endregion

        public virtual object Clone()
        {
            return this.MemberwiseClone();
        }
    }
}
